package com.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.bean.GradeBean;
import com.project.bean.LogBean;
import com.project.bean.RoleBean;
import com.project.bean.UserBean;
import com.project.mapper.IGradeMapper;
import com.project.mapper.ILogMapper;
import com.project.mapper.IRoleMapper;
import com.project.mapper.IUserMapper;

import com.project.service.IRoleService;
import com.project.service.IUserService;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class UserServiceImpl implements IUserService {
    @Autowired
    private IUserMapper userMapper;
    @Autowired
    private IRoleMapper roleMapper;
    @Autowired
    private IGradeMapper gradeMapper;
    @Autowired
    private ILogMapper logMapper;

    @Override
    public UserBean login(String account, String password) {
        QueryWrapper<UserBean> qw = new QueryWrapper<>();
        qw.eq("u_account", account);
        qw.eq("u_password", password);
        UserBean userBean = userMapper.selectOne(qw);
        if (userBean != null) {
            Integer userId = userBean.getId();
            List<GradeBean> gradeList = gradeMapper.findByUserId(userId);
            userBean.setGradeBeans(gradeList);
            Date day=new Date();
            SimpleDateFormat sdf= new SimpleDateFormat("HH:mm");
            String str ="";
            str+="用户："+userBean.getAccount()+"在"+sdf.format(day)+"登陆";
            LogBean logBean = new LogBean(LocalDate.now(),str);
            logMapper.insert(logBean);
        }
        return userBean;
    }


    @Override
    public IPage<UserBean> cutByItem(Integer pageNO, String name) {
        QueryWrapper<UserBean> qw = new QueryWrapper();
        if (name!=null && name.length()!=0){
            qw.like("u_account", name);
        }
        return userMapper.selectPage(new Page<>(pageNO, 3), qw);
    }

    @Override
    public void del(Integer Id) {
        userMapper.delUserRole(Id);
        userMapper.deleteById(Id);
    }



    @Override
    public UserBean findById(Integer id) {
       UserBean userBean =  userMapper.selectById(id);
        Integer userId = userBean.getId();
        List<GradeBean> gradeList = gradeMapper.findByUserId(userId);
        userBean.setGradeBeans(gradeList);
        List<RoleBean> roleBeanList =roleMapper.findByUserId(userId);
        userBean.setRoleBeans(roleBeanList);
        return userBean;
    }



    @Override
    public void update(Integer id, Integer[] arr) {
        userMapper.delUserRole(id);
        userMapper.addUserRole(id,arr);
    }



    @Override
    public void add(UserBean userBean, Integer[] arr) {
        userMapper.insert(userBean);
        userMapper.addUserRole(userBean.getId(),arr);
    }

    @Override
    public void changePWD(Integer id, String password) {
        UserBean userBean= userMapper.selectById(id);
        userBean.setPassword(password);
        userMapper.updateById(userBean);
    }
}
